package com.itheima;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.stream.Stream;

public class FileIOTest {
    @Test
    public void testFile() {
//        File file = new File("D:\\Work\\code\\java\\springboot\\base-study\\src\\main\\java\\com\\itheima\\fileTest.txt");
        File file = new File("src\\main\\java\\com\\itheima\\fileTest.txt");
        System.out.println(file.length());
        System.out.println("是否存在："+file.exists());
        System.out.println("是不是文件"+file.isFile());
        System.out.println("是不是文件夹"+file.isDirectory());
        System.out.println("文件名"+file.getName());
        System.out.println("最后修改时间："+file.lastModified());
    }

    @Test
    public void testCreateFile() throws IOException {
        File file = new File("src\\main\\java\\com\\itheima\\fileTest2.txt");
        System.out.println(file.createNewFile());

        File f2 = new File("src\\main\\java\\com\\itheima\\aa");
        System.out.println(f2.mkdir());

        File f3 = new File("src\\main\\java\\com\\itheima\\bb\\cc");
        System.out.println(f3.mkdirs());

        System.out.println(f2.delete());
    }

    @Test
    public void testGetFiles() {
        File f1 = new File("src\\main\\java\\com\\itheima");
        String[] names = f1.list();
        Stream<String> stream = Arrays.stream(names);
        stream.forEach(System.out::println);

        // listFiles 获取目录下所有文件对象
        // renameTo 修改文件名字
    }

    @Test
    public void testCharset() throws UnsupportedEncodingException {
        String data = "a我b";
        byte[] bytes = data.getBytes("GBK");
        System.out.println(Arrays.toString(bytes));
        // 解码
        String s = new String(bytes, "GBK");
        System.out.println(s);
    }
}
